﻿Shader "Hidden/UIImageGradient"
{
	Properties
	{
		[PreRendererData]_MainTex ("Sprite Texture", 2D) = "white" {}
		_StartColor("Start Color",Color ) = (1,1,1,1)
		_EndColor("End Color",Color ) = (1,1,1,1)
		[Toggle(VerticalDirection)] _VerticalDirection("Vertical Direction" ,Int)  = 0

		[HideInInspector]_StencilComp("Stencil Comparison",Float) = 8
		[HideInInspector]_Stencil("Stencil Id",Float) = 0
		[HideInInspector]_StencilOp("Stencil Operation",Float) = 0
		[HideInInspector]_StencilWriteMask("Stencil Write Mask",Float) = 255
		[HideInInspector]_StencilReadMask("Stencil Read Mask",Float) = 255

		[HideInInspector]_ColorMask("Color Mask",Float) = 15
		[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip("UseAlpha Clip",Float) = 0

	}
	SubShader
	{
		Tags{
			"Queue" = "Transparent"
			"IgnoreProjector" = "True"
			"RenderType" = "Transparent"
			"PreviewType" = "Plane"
			"CanUseSpriteAltas" = "True"

		}
		Stencil{
			Ref[_Stencil]
			Comp[_StencilComp]
			Pass[_StencilOp]
			ReadMask[_StencilReadMask]
			WriteMask[_StencilReadMask]
		}
		// No culling or depth
		Cull Off 
		Lighting off
		ZWrite Off 
		ZTest [unity_GUIZTestMode]
		Blend SrcAlpha OneMinusSrcAlpha
		ColorMask [_ColorMask]



		Pass
		{
			name "UIImage_Gradient" 
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma target 2.0

			#include "UnityCG.cginc"
			#include "UnityUI.cginc"

			#pragma multi_compile __ UNITY_UI_ALPHACLIP


			struct appdata_t
			{
				float4 vertex : POSITION;
				float4 color : COLOR ; 
				float2 texcoord : TEXCOORD0;
				UNITY_VERTEX_INPUT_INSTANCE_ID
			};

			struct v2f
			{
				float4 color : COLOR ; 
				float2 texcoord : TEXCOORD0;
				float4 vertex : SV_POSITION;
				float4 worldPosition : TEXCOORD1 ;
				UNITY_VERTEX_OUTPUT_STEREO
			};

			uniform sampler2D _MainTex;
			uniform float4 _ClipRect;
			uniform fixed4 _TextureSampleAdd;
			uniform fixed4 _StartColor;
			uniform fixed4 _EndColor;
			uniform int _VerticalDirection;

			v2f vert (appdata_t IN)
			{
				v2f OUT;
				UNITY_SETUP_INSTANCE_ID(IN);
				UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
				OUT.worldPosition = IN.vertex;
				OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);
				OUT.texcoord = IN.texcoord;
				OUT.color = IN.color;
				return OUT;
			}
			
			

			fixed4 frag (v2f IN) : SV_Target
			{
				float4 tex = tex2D(_MainTex,IN.texcoord);

				float validFactor = IN.texcoord.x;

				if (_VerticalDirection  != 0){
					validFactor = IN.texcoord.y ;
				};
                
                float4 validBlendColor = lerp(_StartColor,_EndColor,validFactor) * IN.color;

                fixed4 color = tex * validBlendColor ;
                color.a *= UnityGet2DClipping(IN.worldPosition.xy,_ClipRect);

                #ifdef UNITY_UI_ALPHACLIP
					clip(color.a - 0.001);
                #endif
				return color;
			}
			ENDCG
		}
	}
}
